﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoSoftGlobal;

namespace McDonalds_v1._0
{
    public partial class _default : System.Web.UI.Page
    {
        McDonaldsDataContext db = new McDonaldsDataContext();
        public List<Product> productlist = new List<Product>();
        public List<Menu> menulist = new List<Menu>();
        public string pathToNode; 

        protected void Page_Load(object sender, EventArgs e)
        {
            checkQueryString();
            var menus = from menu in db.MENUs select menu;
            foreach (var menu in menus)
            {
                Menu newmenu = new Menu();
                newmenu.Naam = menu.MENUNAAM;
                newmenu.Menunr = menu.MENUNR.ToString();
                menulist.Add(newmenu);
            }

            if (!IsPostBack)
            {
                var categories = from product in db.PRODUCTs
                               group product by product.CATAGORIE into g
                               select new { Categorie = g.FirstOrDefault().CATAGORIE, Id = g.FirstOrDefault().PRODUCTNR, Naam = g.FirstOrDefault().NAAM };
                var products = from product in db.PRODUCTs select product;

                foreach (var categorie in categories)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = categorie.Categorie;
                    newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                    foreach (var product in products)
                    {
                        if (newNode.Text == product.CATAGORIE)
                        {
                            TreeNode productNode = new TreeNode();
                            productNode.NavigateUrl = "http://localhost:54805/Producten.aspx?productnr=" + product.PRODUCTNR.ToString() +"&path="+Server.UrlEncode(product.CATAGORIE);
                            productNode.Text = product.NAAM;
                            productNode.Value = product.PRODUCTNR.ToString();
                            newNode.ChildNodes.Add(productNode);
                        }
                    }
                    MenuTree.Nodes.Add(newNode);
                    MenuTree.CollapseAll();
                }


                if (!string.IsNullOrEmpty((Server.UrlDecode(Request.QueryString["path"]))))
                {
                    TreeNode findNode = MenuTree.FindNode(Server.UrlDecode(Request.QueryString["path"]));
                    if (findNode != null)
                    {
                        findNode.Expand();
                    }
                }
                
                Literal1.Text = FusionCharts.RenderChart("Charts/Column3D.swf", "http://localhost:54805/WebService1.asmx/productXMLtoFSXML", "", "myFirst", "400", "300", false, true);
            }


        }

        public void Select_Change(Object sender, EventArgs e)
        {

            pathToNode = MenuTree.SelectedNode.ValuePath;

        }

        protected void checkQueryString()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["productnr"]))
            {
                int productnr = Int32.Parse(Request.QueryString["productnr"]);
                product_img.Visible = true;
                getProduct(productnr);
                Literal1.Visible = false;
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["menunr"]))
            {
                int menunr = Int32.Parse(Request.QueryString["menunr"]);
                getMenu(menunr);
                Literal1.Visible = false;
            }
            else
            {
                product_img.Visible = false;
            }

        }

        protected void getProduct(int productnr)
        {
            //product_img.Visible = true;
            // Query ingredienten ophalen
            var result = from i in db.INGREDIENTs
                               join pi in db.PRODUCTINGREDIENTENs on i.INGREDIENTNR equals pi.INGREDIENTNR
                               where pi.PRODUCTNR == productnr
                         select new Ingredient { Ingredienten = i.NAAM, Calorien = i.CALORIEN };
            Ingredienten.DataSource = result;
            Ingredienten.DataBind();

            // Query product img ophalen
            var imgresult = from m in db.MEDIAs
                            join mi in db.MEDIAPRODUCTs on m.MEDIANR equals mi.MEDIANR
                            where mi.PRODUCTNR == productnr
                            select m.LINK;

            try
            {
                 Aantal.Visible = true;
                 aantal_txt.Visible = true;
                 bestel_knop.Visible = true;
                 product_img.Visible = true;
                 product_img.ImageUrl = "/images/" + imgresult.First();                 
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught", ex);
            }

        }

        protected void getMenu(int menunr)
        {
            var result = from i in db.INGREDIENTs
                        join pi in db.PRODUCTINGREDIENTENs on i.INGREDIENTNR equals pi.INGREDIENTNR
                        join p in db.PRODUCTs on pi.PRODUCTNR equals p.PRODUCTNR
                        join ms in db.MENUSAMENSTELLINGs on p.PRODUCTNR equals ms.PRODUCTNR
                        join m in db.MENUs on ms.MENUNR equals m.MENUNR
                        where m.MENUNR == menunr
                        select new Ingredient { Ingredienten = i.NAAM, Calorien = i.CALORIEN };
            
            Ingredienten.DataSource = result;
            Ingredienten.DataBind();

        }


        protected int insertOrder()
        {
            ORDER ord = new ORDER
            {
                PERSONEELSNR = 1,
                DATUM = DateTime.Now,
                TAFELNR = 1
            };

            db.ORDERs.InsertOnSubmit(ord);
            db.SubmitChanges();

            return ord.ORDERNR;
        }

        protected void bestel_product(object sender, EventArgs e)
        {
            int ordernr = insertOrder();
            int productnr = Int32.Parse(Request.QueryString["productnr"]);
            int aantal = Int32.Parse(Aantal.Text);

            PRODUCTORDER prod = new PRODUCTORDER
            {
                ORDERNR = ordernr,
                PRODUCTNR = productnr,
                AANTAL = aantal
            };

            db.PRODUCTORDERs.InsertOnSubmit(prod);
            db.SubmitChanges();
            succesmelding.Visible = true;

        }
    }
}